// subpackage/pages/userRegisterV2/stepTwo.js
import requestUtil from '../../../baseRequest'
import urls from '../../../requestUrl'
import commentUtils from '../../../utils/util.js'
import { $Message } from '../../../components/base/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topImage:urls.baseImagePrefixUrl+'/7dfb9ad8-8a3e-4a47-9a8a-3f0572e21a7f.png',
    showEmergency:false,
    registerInfo:{},
    showLoading:false,
  },
  getWxAddress(){
    wx.chooseAddress({
      success: (res)=>{
        this.setData({
          registerInfo:{
            employeeName : res.userName,
            telPhone:res.telNumber,
          },
          provinceCity:res.provinceName+res.cityName+res.countyName,
          detailAddress:res.detailInfo,
          provinceName:res.provinceName,
          cityName:res.cityName,
          countyName:res.countyName,
        });
      },
    })
  },
  toRegionChange(e){
    const regionNameList = [...e.detail.value, "", "", ""];
    console.log(this.data.registerInfo);
    this.setData({
      provinceCity:regionNameList[0]+regionNameList[1]+regionNameList[2],
      provinceName:regionNameList[0],
      cityName:regionNameList[1],
      countyName:regionNameList[2],
    })
  },
  applyRegister(event){
    let formData = event.detail.value;
    if(this.checkFormData(formData)){
      let memberInfo = {
        ...formData,
        openid:wx.getStorageSync('openid'),
        address:this.data.provinceCity+formData.detailAddress,
        phone:formData.telPhone,
        distributeCode:getApp().getDistributeCode(),
        provinceName:this.data.provinceName,
        cityName:this.data.cityName,
        countyName:this.data.countyName,
      }
      wx.setStorageSync('registerInfo', memberInfo);
      wx.redirectTo({
        url: '/subpackage/pages/userRegisterV2/stepThree',
      })
    }
  },

  checkFormData(data){
    if (!data.employeeName || data.employeeName == '') {
      this.showErrorMessage('姓名不能为空')
      return false
    }
    if(data.employeeName.indexOf(" ")>-1){
      this.showErrorMessage('姓名不能含有空格')
      return false
    }
    if(commentUtils.isEmoji(data.employeeName)){
      this.showErrorMessage('姓名不能含有表情字符')
      return false
    }
    if (!data.telPhone || data.telPhone == '') {
      this.showErrorMessage('手机号码不能为空')
      return false
    }
    if(data.telPhone.indexOf(" ")>-1){
      this.showErrorMessage('手机号码不能含有空格')
      return false
    }
    if (!commentUtils.checkTelphoneNumber(data.telPhone)) {
      this.showErrorMessage('手机号码不符合要求')
      return false
    }
    if (!this.data.provinceCity || this.data.provinceCity == '') {
      this.showErrorMessage('地址省市县不能为空')
      return false
    }
    if(commentUtils.isEmoji(this.data.provinceCity)){
      this.showErrorMessage('地址省市县不能含有表情字符')
      return false
    }
    if (!this.data.detailAddress || this.data.detailAddress == '') {
      this.showErrorMessage('详细地址不能为空')
      return false
    }
    if(commentUtils.isEmoji(this.data.detailAddress)){
      this.showErrorMessage('详细地址不能含有表情字符')
      return false
    }
    if(this.data.showEmergency){
      if (!data.emergencyName) {
        this.showErrorMessage('紧急联系人不能为空')
        return false
      }
      if(data.emergencyName.indexOf(" ")>-1){
        this.showErrorMessage('紧急联系人姓名不能含有空格')
        return false
      }
      if(commentUtils.isEmoji(data.emergencyName)){
        this.showErrorMessage('紧急联系人不能含有表情字符')
        return false
      }
      if (!data.emergencyPhone || !commentUtils.checkTelphoneNumber(data.emergencyPhone)) {
        this.showErrorMessage('紧急联系方式不符合要求')
        return false
      }
    }
    return true;
  },
  showErrorMessage(msg) {
    $Message({
      content: msg,
      type: 'error'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})